﻿using BCCommon;
using BCData.TMS.MoveHouseConfig.MoveHouseSpecificationServiceConfigOverWrite;
using BCDto.Sim.Admin.Admin;
using BCDto.TMS.MoveHouseConfig.MoveHouseSpecificationServiceConfigOverWrite;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;

namespace BCService.TMS.MoveHouseConfig.MoveHouseSpecificationServiceConfig
{
    public class MoveHouseSpecificationServiceConfigOverWriteService : IMoveHouseSpecificationServiceConfigOverWriteService
    {
        #region var
        private readonly IMoveHouseSpecificationServiceConfigOverWriteData moveHouseSpecificationServiceConfigOverWriteData;
        private readonly IRedisService redisService;
        private readonly string keyOverWrite = string.Format("{0}", EntityNameConst.MoveHouseSpecificationServiceConfigOverWriteEntity);
        #endregion

        #region 构造函数
        public MoveHouseSpecificationServiceConfigOverWriteService(IMoveHouseSpecificationServiceConfigOverWriteData moveHouseSpecificationServiceConfigOverWriteData,
            IRedisService redisService)
        {
            this.moveHouseSpecificationServiceConfigOverWriteData = moveHouseSpecificationServiceConfigOverWriteData;
            this.redisService = redisService;
        }
        #endregion

        #region Delete
        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="moveHouseSpecificationServiceConfigOverWriteId"></param>
        /// <param name="admin"></param>
        /// <returns></returns>
        public bool DeleteAsync(long moveHouseSpecificationServiceConfigOverWriteId, AdminDto admin)
        {
            if (admin.AgentRecord == null)
            {
                throw new ArgumentException(" 用户认证失败,错误操作 ");
            }
            var entity = moveHouseSpecificationServiceConfigOverWriteData.GetEntityAsync(moveHouseSpecificationServiceConfigOverWriteId).Result;
            if (entity == null)
            {
                throw new ArgumentException(" 获取规格服务信息失败,请检查数据 ");
            }
            var result= moveHouseSpecificationServiceConfigOverWriteData.DeleteAsync(moveHouseSpecificationServiceConfigOverWriteId).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(keyOverWrite, false);
            return result > 0;
        }
        #endregion

        #region moveHouseSpecificationServiceConfigOverWriteId
        /// <summary>
        /// 获取movehousespecificationserviceconfigoverwrite记录
        /// </summary>
        /// <param name="moveHouseSpecificationServiceConfigOverWriteId"></param>
        /// <returns></returns>
        public MoveHouseSpecificationServiceConfigOverWriteDto GetById(long moveHouseSpecificationServiceConfigOverWriteId)
        {
            string cacheKey = this.redisService.GetKey(keyOverWrite, moveHouseSpecificationServiceConfigOverWriteId);
            var resultRedis = this.redisService.TryGet(cacheKey, () =>
            {
                var result = moveHouseSpecificationServiceConfigOverWriteData.GetEntityAsync(moveHouseSpecificationServiceConfigOverWriteId).GetAwaiter().GetResult();
                return result.As<MoveHouseSpecificationServiceConfigOverWriteDto>();
            }, TimeSpan.FromDays(1));
            return resultRedis;
        }

        public bool AgentPrice(long moveHouseSpecificationServiceConfigOverWriteId, decimal price, AdminDto admin)
        {
            if (admin.AgentRecord==null)
            {
                throw new ArgumentException(" 用户认证失败,错误操作 ");
            }
            var entity = moveHouseSpecificationServiceConfigOverWriteData.GetEntityAsync(moveHouseSpecificationServiceConfigOverWriteId).Result;
            if (entity == null)
            {
                throw new ArgumentException(" 获取规格服务信息失败,请检查数据 ");
            }
            entity.Price = price;
            entity.UpdateTime = DateTime.Now;
            entity.EditAdminId = admin.AdminId;
            entity.EditAdminName = admin.FullName;
            var result = moveHouseSpecificationServiceConfigOverWriteData.UpdateAsync(entity).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(keyOverWrite, false);
            return result != null;
        }

        public bool Enabled(long moveHouseSpecificationServiceConfigOverWriteId, AdminDto admin)
        {
            if (admin.AgentRecord == null)
            {
                throw new ArgumentException(" 用户认证失败,错误操作 ");
            }
            var entity = moveHouseSpecificationServiceConfigOverWriteData.GetEntityAsync(moveHouseSpecificationServiceConfigOverWriteId).Result;
            if (entity == null)
            {
                throw new ArgumentException(" 获取规格服务信息失败,请检查数据 ");
            }
            entity.Enabled = true;
            entity.UpdateTime = DateTime.Now;
            entity.EditAdminId = admin.AdminId;
            entity.EditAdminName = admin.FullName;
            var result = moveHouseSpecificationServiceConfigOverWriteData.UpdateAsync(entity).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(keyOverWrite, false);
            return result != null;
        }

        public bool Disabled(long moveHouseSpecificationServiceConfigOverWriteId, AdminDto admin)
        {
            if (admin.AgentRecord == null)
            {
                throw new ArgumentException(" 用户认证失败,错误操作 ");
            }
            var entity = moveHouseSpecificationServiceConfigOverWriteData.GetEntityAsync(moveHouseSpecificationServiceConfigOverWriteId).Result;
            if (entity == null)
            {
                throw new ArgumentException(" 获取规格服务信息失败,请检查数据 ");
            }
            entity.Enabled = false;
            entity.UpdateTime = DateTime.Now;
            entity.EditAdminId = admin.AdminId;
            entity.EditAdminName = admin.FullName;
            var result = moveHouseSpecificationServiceConfigOverWriteData.UpdateAsync(entity).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(keyOverWrite, false);
            return result != null;
        }
        #endregion

    }
}